<template>
  <div class="pb50">
    <el-form size="small" ref="form" :model="form" label-width="150px">
      <!--基础设置-->
      <Basic :form="form"></Basic>
      <!--活动时间-->
      <Datetime :form="form"></Datetime>
      <!--活动商品-->
      <ProductIndex :form="form"></ProductIndex>
      <!--其它-->
      <Other :form="form"></Other>
    </el-form>

    <!--提交-->
    <div class="common-button-wrapper">
      <el-button size="small" @click="cancelFunc">取消</el-button>
      <el-button size="small" type="primary" @click="onSubmit" :loading="loading">提交</el-button>
    </div>
  </div>
</template>
<script>
import SeckillApi from "@/api/seckill.js";
import Basic from "./part/Basic.vue";
import Datetime from "./part/Datetime.vue";
import ProductIndex from "./part/Product.vue";
import Other from "./part/Other.vue";
import dayjs from 'dayjs'
export default {
  components: {
    /*基础设置*/
    Basic,
    /*活动时间*/
    Datetime,
    /*活动商品*/
    ProductIndex,
    /*其它*/
    Other,
  },
  data() {
    return {
      /*表单对象*/
      form: {
        activeRange: [],
        imageId: 0,
        /*活动日期*/
        startDate: "",
        endDate: "",
        /*活动时间*/
        activeTime: "",
        /*标题*/
        title: "",
        /*是否生效，默认1为生效，0为不生效*/
        status: 1,
        /*默认排序*/
        sort: 100,
        /*商品源数据*/
        tableData: [],
        // 拼团类型
        groupType: 1,
        /*商品列表*/
        productList: [],
        activityType: 2,//活动类型
        groupType: 10  //拼团类型 10-普通拼团 20-阶梯拼团 30-多商品拼团
      },
      /*是否正在加载*/
      loading: false,
    };
  },
  created() { },
  methods: {
    /*提交表单*/
    onSubmit() {
      let self = this;
      if (self.form.activityType !== 2) {
        // actStart = self.form.activeTime[0]
        // actEnd = self.form.activeTime[1]
        if (!self.form.activeTime) {
          ElMessage({
            message: "请选择每天活动时间",
            type: "error",
          });
          return;
        }
      }
      if (self.form.activeRange && self.form.activeRange.length > 0) {
        self.form.startDate = self.form.activeRange[0]
        self.form.endDate = self.form.activeRange[1]
      }
      //判断是否选择商品
      if (self.form.tableData.length <= 0) {
        ElMessage({
          message: "请选择活动商品",
          type: "error",
        });
        return
      }
      //判断活动商品
      if (self.form.tableData.length <= 0) {
        ElMessage({
          message: "请选择活动商品",
          type: "error",
        });
        return
      }
      let isBreak = false;
      let message = ''
      for (let i = 0; i < self.form.tableData.length; i++) {
        let item = self.form.tableData[i]
        let j = i + 1
        //验证活动库存 
        if (Number(item.seckillStock) <= 0) {
          isBreak = true
          message = `第${j}行商品活动库存异常`
          break;
        }
        if (self.form.activityType === 4) {
          //预售活动
          if (item.batchList.length <= 0) {
            isBreak = true
            message = '预售活动，请检查所选择的产品批次是否填写'
            break;
          }
          for (let f = 0; f < item.batchList.length; f++) {
            let batchItem = item.batchList[f]
            if (!batchItem.batchNo) {
              ElMessage({
                message: '请检查批次中的提货时间',
                type: "error",
              });
              return
            }
            if (Number(batchItem.batchStock) <= 0) {
              ElMessage({
                message: '请检查批次中的库存',
                type: "error",
              });
              return
            }
          }
        }
        if (self.form.activityType === 2 || self.form.activityType === 3) {
          if (!item.deliveryTime) {
            isBreak = true
            message = `第${j}行商品提货时间异常`
            break;
          }
        }
      }

      if (isBreak) {
        ElMessage({
          message: message,
          type: "error",
        });
        return
      }
      self.$refs.form.validate((valid) => {
        if (valid) {
          let params = self.form;
          params.productList = self.form.tableData
          params.dayStartTime = params.activeTime[0];
          params.dayEndTime = params.activeTime[1];
          SeckillApi.addActive(params, true)
            .then((data) => {
              self.loading = false;
              ElMessage({
                message: "恭喜你，添加成功",
                type: "success",
              });
              self.cancelFunc();
            })
            .catch((error) => {
              self.loading = false;
            });
        }
      });
    },

    /*表格数据格式化*/
    tableFormet(list) {
      let specList = [];
      let newList = [];
      let curItem = null;
      for (let i = 0; i < list.length; i++) {
        let item = list[i];
        let obj = {
          productSkuId: item.productSkuId,
          seckillStock: item.seckillStock,
          seckillPrice: item.seckillPrice,
          productAttr: item.specName,
          seckillProductSkuId: item.seckillProductSkuId,
          productPrice: item.productPrice,
          salesInitial: item.salesInitial,
        };
        if (curItem == null) {
          curItem = item;
        }
        if (curItem.productId != item.productId) {
          curItem.specList = specList;
          newList.push(curItem);
          curItem = item;
          specList = [];
        }
        specList.push(obj);
      }
      curItem.specList = specList;
      newList.push(curItem);
      return newList;
    },

    /*取消*/
    cancelFunc() {
      this.$router.back(-1);
    },
  },
};
</script>
<style scoped>
::v-deep(.el-scrollbar__bar.is-horizontal) {
  height: 20px;          /* 想多粗就写多大 */
}
</style>